package com.spark.rectify

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.google.gson.Gson
import com.spark.colliery.common.ext.watchWithClear
import com.spark.common.BaseActivity
import com.spark.common.Const
import com.spark.common.ext.report
import com.spark.common.ext.wrapIO
import com.spark.common.net.Http
import com.spark.common.net.toJsonBody
import io.reactivex.Observable
import kotlinx.android.synthetic.main.frag_unsafe_action_resp_man.*
import okhttp3.OkHttpClient
import okhttp3.Request

/**
 * 选择不安全行为
 * 责任人
 */
class UnsafeActionRespManActivity : BaseActivity() {
    private lateinit var unsafeActionRespManAdapter: UnsafeActionRespManAdapter

    companion object {
        fun actionStartForBack(host: Activity, reqCode: Int) {
            val intent = Intent(host, UnsafeActionRespManActivity::class.java)
            host.startActivityForResult(intent, reqCode)
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_unsafe_action_resp_man
    }

    @SuppressLint("CheckResult")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        tv_title.text = "选择不安全行为"
        iv_back.setOnClickListener { finish() }
        recy_items.apply {
            val 后勤 = LevelOne(name = "后勤管理处")
            val 一部 = LevelTwo(name = "1部")
            val 二部 = LevelTwo(name = "2部")

            val 张三 = LevelThree(name = "张三", phone = "1234")
            val 李四 = LevelThree(name = "李四", phone = "1234")
            一部.apply {
                addSubItem(张三)
                addSubItem(李四)
            }
            二部.apply {
                addSubItem(张三)
                addSubItem(李四)
            }
            后勤.apply {
                addSubItem(一部)
                addSubItem(二部)
            }
            val 矿井 = LevelOne(name = "矿井管理部")
            矿井.apply {
                addSubItem(一部)
                addSubItem(二部)
            }
            val 信息技术 = LevelOne(name = "信息技术部")
            信息技术.apply {
                addSubItem(一部)
                addSubItem(二部)
            }
            unsafeActionRespManAdapter = UnsafeActionRespManAdapter(mutableListOf(后勤, 矿井, 信息技术))
            adapter = unsafeActionRespManAdapter
            layoutManager = LinearLayoutManager(context)
        }
        // 搜索
        et_search.watchWithClear(iv_delete)
        btn_save.setOnClickListener {
            val results = unsafeActionRespManAdapter.results
            val gson = Gson()
            val backIntent = Intent()
            backIntent.putExtra("results", gson.toJson(results))
            setResult(0x221, backIntent)
            finish()
        }
    }

    @SuppressLint("CheckResult")
    private fun fetchRespMans() {
        Http.request()
                .rxPost("${Const.base_url}${Const.act_rectify_unsafe_action_resp_man}", "{}".toJsonBody())
                .wrapIO()
                .subscribe(
                        {
                            Gson().fromJson(it, UnsafeActionRespManListModel::class.java)?.apply {

                            }
                        },
                        {
                            it.printStackTrace()
                            report(it)
                            loading()
                        },
                        {
                            hideLoader()
                        },
                        {
                            dispose = it
                            loading()
                        }
                )
    }
}


data class UnsafeActionRespManListModel(
        var msg: String = "", // 成功
        var code: String = "", // 200
        var data: Data = Data(),
        var resultMaps: Any = Any() // null
) {

    data class Data(
            var departmentlist: List<Departmentlist> = listOf(),
            var url: String = "" // http://192.168.1.183:8888/safety/
    ) {

        data class Departmentlist(
                var id: Int = 0, // 6
                var departmentId: Int = 0, // 27
                var flags: Int = 0, // 1
                var remark: Any = Any(), // null
                var userlist: List<Userlist> = listOf(),
                var departmentleader: Departmentleader = Departmentleader(),
                var depName: String = "", // 安全科
                var depno: String = "" // 03
        ) {

            data class Userlist(
                    var userid: Int = 0, // 20
                    var username: Any = Any(), // null
                    var employeeno: String = "", // 0003
                    var name: String = "", // 袁建
                    var mobile: String = "", // 15229221916
                    var idcard: String = "", // 919191919191919191
                    var sex: Any = Any(), // null
                    var pictureId: Any = Any(), // null
                    var leader: Int = 0, // 0
                    var id: Int = 0, // 20
                    var picurl: Any = Any() // null
            )


            data class Departmentleader(
                    var userid: Int = 0, // 18
                    var username: Any = Any(), // null
                    var employeeno: String = "", // 0001
                    var name: String = "", // 张无忌
                    var mobile: String = "", // 15229221918
                    var idcard: String = "", // 610102198402062366
                    var sex: Any = Any(), // null
                    var pictureId: Int = 0, // 134
                    var leader: Int = 0, // 1
                    var id: Int = 0, // 18
                    var picurl: String = "" // /pluploadDir/15307974026721.jpg
            )
        }
    }
}